package test;

import common.Utils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;

public class ResetPage extends Utils {
    private static String URL = "http://113.44.92.31:65533/forgot-password.html";

    public ResetPage() {
        super(URL);
    }
    public void checkResetElements() {
        assert driver.findElement(By.cssSelector("body > div > div > div.card.card-md > div > h2")).getText().equals("重置密码");
        assert driver.findElement(By.cssSelector("#resetForm > div:nth-child(1) > label")).getText().equals("邮箱");
        assert driver.findElement(By.cssSelector("#resetForm > div:nth-child(2) > label")).getText().equals("验证码");
        assert driver.findElement(By.cssSelector("#resetForm > div:nth-child(3) > label")).getText().equals("新密码");
        assert driver.findElement(By.cssSelector("#resetForm > div:nth-child(4) > label")).getText().equals("确认密码");
        assert driver.findElement(By.cssSelector("#submit")).getText().equals("重置密码");
        assert driver.findElement(By.cssSelector("#sendCode")).getText().equals("发送验证码");
    }

    /**
     * 修改密码失败 - 验证码为空
     */
    public void resetPasswordFail() {
        driver.findElement(By.cssSelector("#email")).clear();
        driver.findElement(By.cssSelector("#password")).clear();
        driver.findElement(By.cssSelector("#passwordRepeat")).clear();
        driver.findElement(By.cssSelector("#email")).sendKeys("1453837552@qq.com");
        driver.findElement(By.cssSelector("#password")).sendKeys("1111");
        driver.findElement(By.cssSelector("#passwordRepeat")).sendKeys("1111");
        driver.findElement(By.cssSelector("#submit")).click();
        assert wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div"))).getText().split("\n")[2].equals("请填写完整信息");
    }

    /**
     * 修改密码成功
     * @throws InterruptedException
     */
    public void resetPasswordSuccess() throws InterruptedException {
        driver.findElement(By.cssSelector("#email")).clear();
        driver.findElement(By.cssSelector("#password")).clear();
        driver.findElement(By.cssSelector("#passwordRepeat")).clear();

        driver.findElement(By.cssSelector("#email")).sendKeys("1453837552@qq.com");
        driver.findElement(By.cssSelector("#sendCode")).click();
        Thread.sleep(15000);
        //手动获取验证码
        driver.findElement(By.cssSelector("#password")).sendKeys("1111");
        driver.findElement(By.cssSelector("#passwordRepeat")).sendKeys("1111");
        driver.findElement(By.cssSelector("#submit")).click();
        element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div")));
        String text = element.getText();
        String[] lines = text.split("\n");
        String message = lines[2];
        message.equals("密码重置成功，请重新登录");
    }
}
